//
//  MSCheckDetailedController.m
//  觅尚
//
//  Created by No.8 on 2019/3/26.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSRoomDetailedController.h"
#import "MSDayCountController.h"
#import "MSWeekCountController.h"
#import "MSMonthCountController.h"
#import "MSQuarterCountController.h"
#import "MSYearCountController.h"

@interface MSRoomDetailedController ()<UITableViewDelegate,UITableViewDataSource>
{
    CustomNavigationBar *naviagtionBar;
}

/** 选择按钮*/
@property(nonatomic,strong)UIButton *selectedButton;


/** 底部所有的标签 */
@property (nonatomic, strong) UIView  * titlesView;



/** 底部所有的内容 */
@property (nonatomic, strong) UIScrollView  * contenView;

@end

@implementation MSRoomDetailedController

- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = backgroundcolor;
    
    [self CreateNavigationBar];
    
    
    [self CreatView];
    
    //初始化子控制器
    [self setupChildVces];
    
    
    [self setupScrollView];
    
    
    
}
/**
 *底部的scrollview
 */
- (void)setupScrollView
{
    //不自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView * contenView = [[UIScrollView alloc]init];
    contenView.frame = self.view.bounds;
    contenView.delegate = self;
    contenView.pagingEnabled = YES;
    
    [self.view insertSubview:contenView atIndex:0];
    contenView.contentSize = CGSizeMake(contenView.width * self.childViewControllers.count,0);
    
    self.contenView = contenView;
    //
    //    //添加第一个控制器view
    [self scrollViewDidEndScrollingAnimation:contenView];
}

- (void)setupChildVces{
    MSDayCountController * DayVC = [[MSDayCountController alloc] init];
    [self addChildViewController:DayVC];
    
    
    
    MSWeekCountController * WeekVC = [[MSWeekCountController alloc] init];
    [self addChildViewController:WeekVC];
    
    
    MSMonthCountController * MonthVC = [[MSMonthCountController alloc] init];
    [self addChildViewController:MonthVC];
    
    
    
    MSQuarterCountController * QuarterVC = [[MSQuarterCountController alloc] init];
    [self addChildViewController:QuarterVC];
    
    
    MSYearCountController * YearVC = [[MSYearCountController alloc] init];
    [self addChildViewController:YearVC];
    
    
    
}
- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}




- (void)CreatView{
    // 标签栏整体
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = BackViewColor;
    titlesView.frame = CGRectMake(0, [self navBarBottom], ScreenWidth, 150 * px);
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    
    NSArray * titleArray = @[@"日",@"周",@"月",@"季度",@"年"];
    
    CGFloat ButtonWidth = ScreenWidth / titleArray.count;
    CGFloat ButtonHeight = titlesView.height;
    CGFloat ButtonY = 0;
    for (NSInteger i = 0; i < titleArray.count; i ++) {
        UIButton * Button = [[UIButton alloc] init];
        Button.tag = i;
        [Button layoutIfNeeded];//强制布局
        Button.x = ButtonWidth * i;
        Button.y = ButtonY;
        Button.width = ButtonWidth;
        Button.height = ButtonHeight;
        [Button setTitle:titleArray[i] forState:UIControlStateNormal];
        [Button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [Button setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        [Button addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [Button setBackgroundImage:[UIImage imageWithColor:BackViewColor] forState:UIControlStateNormal];
        [Button setBackgroundImage:[UIImage imageWithColor:FontColor] forState:UIControlStateDisabled];
        Button.titleLabel.font = [UIFont systemFontOfSize:28 / 2];
        //        [Button setBackgroundColor:BackViewColor];
        [titlesView addSubview:Button];
        // 默认点击了第一个按钮
        if (i == 0) {
            [self ButtonClick:Button];
            Button.enabled = NO;
            
            self.selectedButton = Button;
        }
    }
    
}
- (void)ButtonClick:(UIButton *)button{
    //修改按钮状态
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    self.selectedButton = button;
    
    //滚动
    CGPoint offset = self.contenView.contentOffset;
    offset.x = button.tag * self.contenView.width;
    [self.contenView setContentOffset:offset animated:YES];
    
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
    //当前的索引
    //长度NSInteger * interger;
    int index = scrollView.contentOffset.x / scrollView.width;
    
    //取出子控制器
    UIViewController * vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = [self navBarBottom] + self.titlesView.height;//tableview的高度默认为20
    vc.view.width = ScreenWidth;
    vc.view.height = scrollView.height;//tableview的底部
    //    //设置内边距
    //
    //    CGFloat bottom = self.tabBarController.tabBar.height;
    //    CGFloat top = CGRectGetMaxY(self.titlesView.frame);
    //    vc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    //    //设置滚动条的内边距
    //    vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
    [scrollView addSubview:vc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //点击按钮
    int index = scrollView.contentOffset.x / scrollView.width;
    [self ButtonClick:self.titlesView.subviews[index]];
    
    
    // 添加子控制器的view
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"统计";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}

@end
